﻿using AutoMapper;
using DLL.NET50.DB.MS.Entity;
using DLL.NET60.DB.MS.Entity.DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DLL.NET60.DB.MS.Entity.Map
{
    public interface Iadi_work_fileProfile
    {
    }

    public class Cadi_work_fileProfile : Profile, Iadi_work_fileProfile
    {
        public Cadi_work_fileProfile()
        {
            CreateMap<adi_work_file, DTO_adi_work_file>()
                .ReverseMap();
        }
    }

    public interface Iadi_dic_drugProfile
    {
    }

    public class Cadi_dic_drugProfile : Profile, Iadi_dic_drugProfile
    {
        public Cadi_dic_drugProfile()
        {
            CreateMap<adi_dic_drug, DTO_adi_dic_drug>()
                .ForMember(dst => dst.flush_Groups, opt => opt.MapFrom(src => src.flush_Groups))
                .ReverseMap();
        }
    }

    public interface Iadi_FlushGroupProfile
    {
    }

    public class Cadi_FlushGroupProfile : Profile, Iadi_FlushGroupProfile
    {
        public Cadi_FlushGroupProfile()
        {
            CreateMap<adi_FlushGroup, DTO_adi_FlushGroup>()
                .ForMember(dst => dst.flush_Params, opt => opt.MapFrom(src => src.flush_Params))
                .ForMember(dst => dst.pumpBack_Params, opt => opt.MapFrom(src => src.pumpBack_Params))
                .ReverseMap();
        }
    }

    public interface Iadi_FlushPramProfile
    {
    }

    public class Cadi_FlushPramProfile : Profile, Iadi_FlushPramProfile
    {
        public Cadi_FlushPramProfile()
        {
            CreateMap<adi_FlushParam, DTO_adi_FlushParam>()
                .ReverseMap();
        }
    }

    public interface Iadi_PumpBackParamProfile
    {
    }

    public class Cadi_PumpBackParamProfile : Profile, Iadi_PumpBackParamProfile
    {
        public Cadi_PumpBackParamProfile()
        {
            CreateMap<adi_PumpBackParam, DTO_adi_PumpBackParam>()
                .ReverseMap();
        }
    }

    public static class DynamicExtensions
    {
        public static dynamic ToDynamic(this object value)
        {
            IDictionary<string, object> expando = new ExpandoObject();
            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
            {
                object val = property.GetValue(value);
                if ((property.PropertyType.IsGenericType || property.PropertyType.Name.Contains("[]")) && val is IEnumerable<object>)
                {
                    var values = val as IEnumerable<object>;
                    List<ExpandoObject> result = new List<ExpandoObject>();
                    foreach (var v in values)
                    {
                        result.Add(v.ToDynamic());
                    }
                    expando.Add(property.Name, result);
                }
                else if (property.PropertyType.Name.Contains("Anonymous"))
                {
                    expando.Add(property.Name, val.ToDynamic());
                }
                else
                {
                    expando.Add(property.Name, val);
                }
            }
            return expando as ExpandoObject;
        }
    }
}